﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;

namespace Dragon
{
    public partial class wfSMSMonthly : System.Web.UI.Page
    {
        DataService ds = new DataService();

        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> roles = new List<string> { "All", "BAM"};
            if (Session["Username"] == null || roles.Contains(Session["UserType"].ToString()) == false)
            {
                Response.Redirect("~/wfLogin.aspx");
            }

            if (!IsPostBack)
            {
                this.ReportViewer1.Visible = false;                 
                this.txtFromDate.Text = "01/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("yyyy");
                this.txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");                
            }
        }

        protected void btnRunReport_Click(object sender, EventArgs e)
        {
            DateTime fromDate = new DateTime();
            DateTime toDate = new DateTime();

            try
            {
                fromDate = DateTime.ParseExact(this.txtFromDate.Text, "dd/MM/yyyy", null);
                toDate = DateTime.ParseExact(this.txtToDate.Text, "dd/MM/yyyy", null);
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "<script>alert('Ngày tháng không hợp lệ!');</script>");
                return;
            }

            string ProvinceCode, ProvinceName;
            if (this.ddlProvince.SelectedIndex != 0)
            {
                ProvinceCode = this.ddlProvince.SelectedValue.ToString();
                ProvinceName = this.ddlProvince.SelectedItem.ToString();
            }
            else
            {
                ProvinceCode = "";
                ProvinceName = "Tất cả";
            }

            string BASCode, BASName;
            if (this.ddlBAS.SelectedIndex != 0)
            {
                BASCode = this.ddlBAS.SelectedValue.ToString();
                BASName = this.ddlBAS.SelectedItem.ToString();
            }
            else
            {
                BASCode = "";
                BASName = "Tất cả";
            }

            int smsType = this.ddlSMSType.SelectedIndex;
            int skuType = this.ddlSKUType.SelectedIndex;

            string sql = "exec sp_Report_SMS_Monthly @FromDate, @ToDate, @ProvinceCode, @BASCode, @Type, @SKUType";
            SqlParameter pFromDate = new SqlParameter("@FromDate", fromDate);
            SqlParameter pToDate = new SqlParameter("@ToDate", toDate);
            SqlParameter pProvinceCode = new SqlParameter("@ProvinceCode", ProvinceCode);
            SqlParameter pBASCode = new SqlParameter("@BASCode", BASCode);
            SqlParameter pType = new SqlParameter("@Type", smsType);
            SqlParameter pSKUType = new SqlParameter("@SKUType", skuType);
            ds.SetData(sql, pFromDate, pToDate, pProvinceCode, pBASCode, pType, pSKUType);

            DataTable dt = ds.GetData("select * from ReportSMSMonthly");
            ReportDataSource rds = new ReportDataSource("dsSMSMonthly", dt);
            this.ReportViewer1.Visible = true;                 
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
        }
 
       protected void BASOnDataBound(object sender, EventArgs e)
        {
            this.ddlBAS.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlBAS.SelectedIndex = 0;
        }

       protected void ProvinceOnDataBound(object sender, EventArgs e)
       {
           this.ddlProvince.Items.Insert(0, new ListItem("Tất cả", ""));
           this.ddlProvince.SelectedIndex = 0;
       }
    }
}